//
//  CacheFunc.swift
//  WaiBao
//
//  Created by 赵佟越 on 17/5/13.
//  Copyright © 2017年 zhaotongyue. All rights reserved.
//

import Foundation
import UIKit
class CacheFunc: NSObject {
    
    /**
     *  获取沙盒路径
     */
    func cachePath(proName:String)->String
    {
        let cachePath = NSHomeDirectory() + "/Library/Caches/proData/" + proName+"/"
        let FM:FileManager = FileManager.default
        //判断当前路径是否存在
        if !FM.fileExists(atPath: cachePath, isDirectory: nil)
        {
            do {
                try FM.createDirectory(atPath: cachePath, withIntermediateDirectories: true, attributes: nil)
            } catch let error as NSError
            {
                print("存储路径错误-->\(error)")
            }
        }
        return cachePath
    }
    //存储缓存
    func saveDataToCache(proName:String,Data:NSData) -> ()
    {
        let pathStr = self.cachePath(proName: proName)+"\(proName).png"
        print("存路径--->\(pathStr)")
        Data.write(toFile: pathStr, atomically: true)
    }
    //取缓存
    func getDataFromCache(proName:String) -> NSData
    {
        let pathStr = self.cachePath(proName: proName)+"\(proName).png"
        print("路径--->\(pathStr)")
        let data:NSData = NSData(contentsOfFile: pathStr)!
        return data
    }
    
    
}
